﻿namespace BEGDC.CC.Web.EZVisit.View
{
    #region Using Directives

    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using ASBCC.Business;
    using Control;
    using Utility.Extensions;
    using Workflow;

    #endregion

    public partial class MyActivities : BasePage
    {
        #region Properties

        private string Sort
        {
            get
            {
                return Convert.ToString(this.ViewState["Sorting"]);
            }

            set
            {
                this.ViewState["Sorting"] = value;
            }
        }

        private int PageCount
        {
            get
            {
                return Convert.ToInt32(this.ViewState["PageCount"]);
            }
            set
            {
                this.ViewState["PageCount"] = value;
            }
        }

        private int CurrentPageIndex
        {
            get
            {
                return Convert.ToInt32(this.ViewState["CurrentPageIndex"]);
            }
            set
            {
                this.ViewState["CurrentPageIndex"] = value;
            }
        }

        private int RowCount
        {
            get
            {
                return Convert.ToInt32(this.ViewState["RowCount"]);
            }
            set
            {
                this.ViewState["RowCount"] = value;
            }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindData(1);
                this.btnReload.Attributes["style"] = "display:none";
            }
        }

        private void BindData(int pageIndex)
        {
            int userId = this.UserInfo.EmployeeID.Value;

            var ezvisitService = new EZVisitBusiness();

            var dt = ezvisitService.GetMyActivities(userId);

            if (this.Sort.IsNotNullOrWhiteSpace())
            {
                dt.DefaultView.Sort = this.Sort;
            }

            int count = dt.DefaultView.Count;

            this.dgDrafts.DataSource = dt.DefaultView;
            this.dgDrafts.DataBind();

            this.SetPageCount(count, pageIndex);
        }

        private void SetPageCount(int count, int pageIndex)
        {
            if (count > 0)
            {
                this.RowCount = count;
                this.PageCount = count / this.dgDrafts.PageSize;

                if (count % this.dgDrafts.PageSize > 0)
                {
                    this.PageCount++;
                }

                this.CurrentPageIndex = pageIndex;
                this.SetPageNavi(pageIndex);
            }
            else
            {
                this.RowCount = count;
                this.PageCount = count;
                this.CurrentPageIndex = 0;
                this.SetPageNavi(0);
            }
        }

        private void SetPageNavi(int newPage)
        {
            if (newPage <= 1)
            {
                this.btnPageFirst.ImageUrl = "~/images/pg_first_inact.gif";
                this.btnPagePrev.ImageUrl = "~/images/pg_pre_inact.gif";
                this.btnPageFirst.Attributes["disabled"] = "true";
                this.btnPagePrev.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageFirst.ImageUrl = "~/images/pg_first.gif";
                this.btnPagePrev.ImageUrl = "~/images/pg_pre.gif";
                this.btnPageFirst.Attributes.Remove("disabled");
                this.btnPagePrev.Attributes.Remove("disabled");
            }

            if (newPage >= this.PageCount)
            {
                this.btnPageNext.ImageUrl = "~/images/pg_next_inact.gif";
                this.btnPageLast.ImageUrl = "~/images/pg_last_inact.gif";
                this.btnPageNext.Attributes["disabled"] = "true";
                this.btnPageLast.Attributes["disabled"] = "true";
            }
            else
            {
                this.btnPageNext.ImageUrl = "~/images/pg_next.gif";
                this.btnPageLast.ImageUrl = "~/images/pg_last.gif";
                this.btnPageNext.Attributes.Remove("disabled");
                this.btnPageLast.Attributes.Remove("disabled");
            }

            this.lblCurrentPageIndex.Text = newPage.ToString();
            this.lblPageCount.Text = this.PageCount.ToString();
            this.lblRecordCount.Text = this.RowCount.ToString();
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            this.dgDrafts.AllowPaging = false;

            this.BindData(1);

            this.dgDrafts.Columns[11].Visible = false;
            this.dgDrafts.Columns[13].Visible = true;
            ExportDataGridToExcel.Export(this.Page, this.dgDrafts, "MyEZVisitActivities.xls");
            this.dgDrafts.Columns[11].Visible = true;
            this.dgDrafts.Columns[13].Visible = false;
        }

        protected void dgDrafts_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (this.Sort.IndexOf(e.SortExpression) > -1 && this.Sort.Equals(e.SortExpression))
            {
                this.Sort = e.SortExpression + " desc";
            }
            else
            {
                this.Sort = e.SortExpression;
            }

            this.BindData(this.CurrentPageIndex);
        }

        protected void btnPageFirst_Click(object sender, ImageClickEventArgs e)
        {
            this.dgDrafts.CurrentPageIndex = 0;
            this.BindData(1);
        }

        protected void btnPagePrev_Click(object sender, ImageClickEventArgs e)
        {
            if (this.CurrentPageIndex == 1)
            {
                return;
            }

            this.dgDrafts.CurrentPageIndex = this.CurrentPageIndex - 2;
            this.BindData(this.CurrentPageIndex - 1);
        }

        protected void btnPageNext_Click(object sender, ImageClickEventArgs e)
        {
            if (this.CurrentPageIndex == this.PageCount)
            {
                return;
            }

            this.dgDrafts.CurrentPageIndex = this.CurrentPageIndex;
            this.BindData(this.CurrentPageIndex + 1);
        }

        protected void btnPageLast_Click(object sender, ImageClickEventArgs e)
        {
            this.dgDrafts.CurrentPageIndex = this.PageCount - 1;
            this.BindData(this.PageCount);
        }

        protected void btnGotoPage_Click(object sender, ImageClickEventArgs e)
        {
            if (this.txtPage.Text.Trim().Length == 0)
            {
                this.txtPage.Text = "1";
            }

            int gotopage;

            if (!int.TryParse(this.txtPage.Text, out gotopage))
            {
                gotopage = 1;
            }

            int currentpage = 0;

            if (gotopage >= this.PageCount)
            {
                gotopage = this.PageCount;
                currentpage = gotopage - 1;
                if (currentpage < 0)
                {
                    currentpage = 0;
                }
            }
            else if (gotopage <= 0)
            {
                gotopage = 1;
            }
            else
            {
                currentpage = gotopage - 1;
            }

            this.dgDrafts.CurrentPageIndex = currentpage;

            this.BindData(gotopage);

            this.txtPage.Text = string.Empty;
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            this.BindData(this.CurrentPageIndex);
        }
    }
}